import toto
import urllib.request,urllib.parse,re,json,time
rcnt=['654','645','735','753','465','834','744','843','438','366','636','456','276','474','825','537','545']
ps=[[0.44, 0.29, 0.27], [0.42, 0.29, 0.29], [0.4, 0.28, 0.32], [0.35, 0.28, 0.37], [0.28, 0.28, 0.44], [0.39, 0.29, 0.32], [0.39, 0.29, 0.32], [0.45, 0.28, 0.27], [0.4, 0.29, 0.31], [0.39, 0.29, 0.32], [0.33, 0.29, 0.38], [0.3, 0.29, 0.41], [0.42, 0.27, 0.31], [0.41, 0.27, 0.32], [0.31, 0.27, 0.42]]
def test(n):
    tm=time.time()
    bets=[]
    ##probs=toto.readprob(r'c:\Users\risubilov\Documents\Portable Python 3.2.5.1\toto\724-2.csv')
    ##r=open(r'c:\Users\risubilov\res7093.txt','w')
    ##r2=open(r'c:\Users\risubilov\res7093_2.txt','w')
    i=0
    k=0
    while (i<n):
        k+=1
        bet=toto.genbet(ps)
##        templ='**1***1*21**1**'
##        for b in bet:templ=templ.replace('*',b,1)
##        bet=templ
        x1=toto.sv(bet,'111111111111111')
        x2=toto.sv(bet,'X'*15)
        x3=toto.sv(bet,'222222222222222')
        x4=toto.sv(bet,'XX1X2X221X11112')
        print(bet)
        if (x1+x2+x3) in rcnt:
            ##r.write(bet+';'+x1+x2+x3+'\n')
            i+=1
##
            request = urllib.request.Request(r"http://toto-info.fonbet.com/DataService.svc/CalculateCoupons")
            request.add_header("Content-Type",r"application/json; charset=UTF-8")
            tm=time.time()
            data = (r'{"options":{"DrawingId":751,"ExtraCoupons":["10000"],"Kind":"coupon","StartFrom":0,"Count":50,"Mask":"'+bet+r'"}').encode('utf-8')
            print(data)
            f = urllib.request.urlopen(request, data)
            print(time.time()-tm)
            resp=f.read().decode('utf-8')
            f.close()
            p=json.loads(resp)
            tmp=[]
            tmp.append(bet)
            for  row in p.get('d').get('Summary').get('CalcResult').get('DrawingResults'): tmp+=[row.get('Count')]
##
            bets.append(tmp)
        else: pass ##r2.write(bet+';'+x4+'\n')
    ##r.close()
   ##r2.close()
    print(k,i)
    print(time.time()-tm ,x1+x2+x3)
    return bets